package cn.gxa.j229.bug.finance.controller.content;//package cn.gxa.j229.bug.finance.controller.content;

import cn.gxa.j229.bug.finance.pojo.Ad;
import cn.gxa.j229.bug.finance.pojo.Loan;
import cn.gxa.j229.bug.finance.service.contant.AdService;
import cn.gxa.j229.bug.finance.utils.JsonResult;
import cn.gxa.j229.bug.finance.utils.PageUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author Jun
 * @version 1.0
 * @date 2020/9/21 14:24
 */

@Controller
@RequestMapping("/ad")
@CrossOrigin
public class AdController {

    @Resource
    AdService adService;

    //添加
    @PostMapping("/add")
    @ResponseBody
    public Map add(@RequestBody Ad ad){
        adService.insert(ad);
        System.out.println(ad);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        jsonResult.setData(ad);
        return jsonResult.getValues();
    }

    //根据标题查询
    @PostMapping("/findByTitle")
    @ResponseBody
    public Map selectByTitle(@RequestBody Ad ad){
        List<Map<String,Object>> list =adService.selectByTitle(ad.getTitle());
        System.out.println(list);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询题目带有" +ad.getTitle()+"的成功");
        jsonResult.setData(list);
        return jsonResult.getValues();
    }

    //根据id查询
    @PostMapping("/findById")
    @ResponseBody
    public Map findById(@RequestBody Ad ad){
        List<Map<String,Object>> list =adService.selectById(ad.getId());
        System.out.println(list);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询id为" +ad.getId()+"的成功");
        jsonResult.setData(list);
        return jsonResult.getValues();
    }
    //根据状态查询
    @PostMapping("/findByState")
    @ResponseBody
    public Map selectByState(@RequestBody Ad ad){
        List<Map<String,Object>> list =adService.selectByState(ad.getState());
        System.out.println(list);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询状态为" +ad.getState()+"的成功");
        jsonResult.setData(list);
        return jsonResult.getValues();
    }

    //根据位置查询
    @PostMapping("/findByPosition")
    @ResponseBody
    public Map findByPosition(@RequestBody Ad ad){
        List<Map<String,Object>> list =adService.selectByPosition(ad.getPosition());
        System.out.println(list);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询位置含有" + ad.getPosition() +"的成功");
        jsonResult.setData(list);
        return jsonResult.getValues();

    }
    //查询全部省份
    @PostMapping("/findAllProvince")
    @ResponseBody
    public Map findAllProvince(){
        List<Map<String,Object>> list =adService.selectAllProvince();
        System.out.println(list);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询全部省份为:" + adService.selectAllProvince() + "的成功");
        jsonResult.setData(list);
        return jsonResult.getValues();
    }
    //根据省份查询
    @PostMapping("/findAllPosition")
    @ResponseBody
    public Map findAllPosition(){
        List<Map<String,Object>> list =adService.selectAllPosition();
        System.out.println(list);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询全部位置为：" + adService.selectAllPosition() + "的成功");
        jsonResult.setData(list);
        return jsonResult.getValues();
    }
    //根据省份查询
    @PostMapping("/findByProvince")
    @ResponseBody
    public Map findAllPosition(@RequestBody Ad ad){
        List<Map<String,Object>> list =adService.selectByProvince(ad.getProvince());
        Integer counts = adService.selectByPageCount(ad);
        System.out.println(list);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询省份为" + ad.getProvince() + "的成功");
        jsonResult.setData(list);
        return jsonResult.getValues();
    }
    //分页查询
    @PostMapping("/findByPage")
    @ResponseBody
    public Map findByPage(@RequestBody Ad ad){

        List<Map<String,Object>> list =adService.selectAdByPage(ad);
        int counts = adService.selectByPageCount(ad);
        System.out.println(list);
        JsonResult jsonResult = new JsonResult();
        jsonResult.put("count",counts);
        jsonResult.setCode("200");
        jsonResult.setMsg("分页查询");
        jsonResult.setData(list);
        return jsonResult.getValues();
    }
    //查询总记录数
    @PostMapping("/findCount")
    @ResponseBody
    public Map findCount(){
        List<Map<String,Object>> count =adService.selectCount();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(count);
        jsonResult.setMsg("成功");
        return jsonResult.getValues();

    }
    //修改
    @PostMapping("/update")
    @ResponseBody
    public Map update(@RequestBody Ad ad){

        int update =adService.update(ad);
        System.out.println(ad);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改了id为" + ad.getId() +"的数据 :" + ad);
        jsonResult.setData(update);
        return jsonResult.getValues();

    }
    //删除
    @PostMapping("/delete")
    @ResponseBody
    public Map delete(@RequestBody Ad ad){
        adService.delete(ad.getId());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        jsonResult.setData("");
        return jsonResult.getValues();
    }


}
